<?php 
namespace App\Controller;

use App\Model\UserModel;
use lib\View;

use App\Model\DynamicModel;
use App\Model\CommentModel;
use App\Model\FriendsModel;
use App\Model\GalleryModel;
use App\Model\PicturesModel;

class GalleryController
{
  
  public function album(){
    if (!isset($_SESSION['user'])) {
      return new View('login');
    } 
    else{
  	  $user= $_SESSION['user'] ;

   $user_id = isset($_SESSION['user_id']) ? $_SESSION['user_id']  : '';
     $count = isset($_SESSION['count']) ? $_SESSION['count'] : '';
   $edit = new UserModel();
   $data = $edit->get(['username' => $user]);
   $_SESSION['pic'] = $data[0]['pic']; 

   	     $fids=[];
        $f=new FriendsModel();
        $frData=$f->get(['uid'=>$_SESSION['user_id']]);
        foreach($frData as $ve){
            $fids[]=$ve['fid'];
        }
        $get_user=new UserModel();
        $userData=$get_user->get();
        //去掉登录用户的信息
        foreach($userData as $k=>$v){
            if($v['id']==$_SESSION['user_id']){
                unset($userData[$k]);
            }
            //去掉好友信息
            if(in_array($v['id'],$fids)){
                unset($userData[$k]);
            }
        }
        //返回好友信息
        $fData=$get_user->join(['friends'=>'fid'],['friends.uid'=>$_SESSION['user_id']]);

        //获取相册列表
        $picture = new PicturesModel();
        $pictures = $picture->get();

        $album = new GalleryModel();
        $albums = $album->get(['users_id'=>$user_id]);

        foreach ($albums as $key => $value) {
          $album_id = $albums[$key]['id'];       
       foreach ($pictures as $key2 => $value2) { 
       if ($value2['album_id'] == $album_id) {
                $albums[$key]['pictureIfo'][]=$value2['path'];

      }
  }
    }  
   		return new View('album',['user'=>$user,'user_id'=>$user_id,'data'=>$data[0],'userData'=>$userData,'fData'=>$fData,'albums'=>$albums,'count'=>$count]);
          }
   }

    public function uploadHeadPicture(Array $files){
     //图片上传部分
 if (!empty($files)) {
    $count = count($files['name']);
    $types = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'];
    $uploadDir = 'upload' . DIRECTORY_SEPARATOR;
    for($i=0;$i<$count;$i++){
    if (in_array($files['type'][$i], $types)) {
        if (($files['size'][$i]> 0) && ($files['size'][$i] < 10 * 1024 * 1024)) {
               $fileName = hash('sha256', mt_rand() . time() . $files['name'][$i]);
               // 上传文件相对于public的相对路径
               $filePath = $uploadDir . $fileName . '.' . pathinfo($files['name'][$i])['extension'];
               $imageId = md5($filePath);
               // 上传文件的绝对路径
                $destination = PUBLIC_DIR . DIRECTORY_SEPARATOR . $filePath;
                move_uploaded_file($files['tmp_name'][$i],$destination);
                $station = '..'.DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.$filePath;
                $pic=str_replace(DIRECTORY_SEPARATOR,'\\\ ',$station );
                $pic=str_replace(' ','',$pic );
                $pics[] =$pic;
                   } 
               }
             
      }
       return $pics;
   }
 }

 public function add_picture(){
    $user =$_SESSION['user'];
    $user_id=$_SESSION['user_id'];
    $files = isset($_FILES['imgs']) ? $_FILES['imgs'] : '';
    $album_id = $_GET['album_id'];

 if (!empty($user)) 
    {
    $pics=$de->uploadHeadPicture($files);
     $save = new PicturesModel();
    foreach ($pics as $pic) {
        $data['album_id'] =$album_id;
        $data['path'] =$pic;
        $data['upload_at'] = date('Y-m-d H:i:s',time());
        $save->save($data);
    }

 }
}


 public function delete_picture(){
  $album_id = $_GET['album_id'];
  $data1['id'] = $album_id;
  $data['album_id'] = $album_id;

  $edit = new PicturesModel();
  $edit ->deletes($data);


  $edit1 = new GalleryModel();
  $edit1 ->deletes($data1);
    success('删除成功','album');

 }


}

